package com.siyu.decorator.logger;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;

import java.util.Arrays;

/**
 * @Author: siyu
 * @CreateTime: 2024-10-06
 * @Version: 1.0
 */
public class JsonLogger extends LoggerDecorator{
    public JsonLogger(Logger logger) {
        super(logger);
    }

    @Override
    public void info(String s) {
        JSONObject jsonObject = newJsonObject();
        jsonObject.put("message",s);
        logger.info(jsonObject.toString());
    }

    @Override
    public void error(String s) {
        JSONObject jsonObject = newJsonObject();
        jsonObject.put("error",s);
        logger.error(jsonObject.toString());
    }

    public void error(Exception e) {
        JSONObject jsonObject = newJsonObject();
        jsonObject.put("error",e.getClass().getName());
        String trace = Arrays.toString(e.getStackTrace());
        jsonObject.put("trace",trace);
        logger.error(jsonObject.toString());
    }

    private JSONObject newJsonObject(){
        return new JSONObject();
    }
}
